package jcu.salt;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Wraps {@link Properties} to provide convenient access to configuration values
 */


public class Configuration {

	private static final String PROPERTIES_FILE = "salt.properties";

	public static final String IP = "IP";

	private static Properties props;

	private final static Configuration INSTANCE = new Configuration();

	public static final String DATATURBINE_IP 	= "DATATURBINE_IP";
	public static final String SOURCE 			= "SOURCE";
	public static final String SINK 			= "SINK";
	public static final String DEFAULTCHANNEL 	= "DEFAULTCHANNEL";
	public static final String ENCODED			= "ENCODED";
	public static final String CLIENT_NAME		= "CLIENT_NAME";
	public static final String CLIENT_IP		= "CLIENT_IP";
	public static final String AGENT_IP			= "AGENT_IP";
	public static final String TYPE_OS			= "TYPE_OS";
	public static final String TYPE_1WIRE		= "TYPE_1WIRE";
	public static final String TYPE_SNMP		= "TYPE_SNMP";
	public static final String TYPE_VIDEO		= "TYPE_VIDEO";
	public static final String ARCHIVESIZE		= "ARCHIVESIZE";
	public static final String ARCHIVEMODE		= "ARCHIVEMODE";
	public static final String CACHESIZE		= "CACHESIZE";
	public static final String SALID 			= "SAL_ID";	
	public static final String SRB_USERNAME		= "SRB_USERNAME";	
	public static final String SRB_DOMAIN		= "SRB_DOMAIN";
	public static final String SRB_PASSWORD		= "SRB_PASSWORD";
	public static final String SRB_HOST			= "SRB_HOST";
	public static final String SRB_RESOURCE		= "SRB_RESOURCE";	

	/**
	 * private to suppress unauthorized calls : SINGLETON
	 */
	private Configuration() {
		props = new Properties(getDefaultProps());
		try {
			props.load(new FileInputStream(PROPERTIES_FILE));
		} catch (IOException e) {
			System.err.println("failed to load properties - will use defaults");
		}
	}

	public static Configuration getInstance() {
		return Configuration.INSTANCE;
	}

	/**
	 * Provides access to the value for a given configuration key
	 * 
	 * @param key
	 * @return the value for the key, if available - otherwise null
	 */
	public String get(String key) {
		try {
			return props.get(key).toString();
		} catch (NullPointerException e) {
		}
		return null;
	}

	public boolean isSet(String key) {
		try {
			String value = props.get(key).toString();
			if (value.toLowerCase().equals("true")) {
				return true;
			}
		} catch (NullPointerException e) {
		}
		return false;
	}

	/**
	 * Initialize a Properties object with some default values.
	 * 
	 * @return the default Properties object
	 */
	private static Properties getDefaultProps() {
		Properties defaults = new Properties();

		defaults.setProperty(DATATURBINE_IP, "137.219.45.205");
		defaults.setProperty(SOURCE, "SALTSRC");
		defaults.setProperty(SINK, "SALTSINK");
		defaults.setProperty(DEFAULTCHANNEL, "saltchannel0");
		defaults.setProperty(DEFAULTCHANNEL, "false");
		defaults.setProperty(CLIENT_NAME, "salt_default");
		defaults.setProperty(CLIENT_IP, "127.0.0.1");
		defaults.setProperty(AGENT_IP, "127.0.0.1");
		defaults.setProperty(TYPE_OS, "10000");
		defaults.setProperty(TYPE_SNMP, "10000");
		defaults.setProperty(TYPE_1WIRE, "10000");
		defaults.setProperty(TYPE_VIDEO, "10000");
		defaults.setProperty(ARCHIVESIZE, "5000");
		defaults.setProperty(ARCHIVEMODE, "20000");
		defaults.setProperty(CACHESIZE, "append");
		defaults.setProperty(SALID, "append");
		defaults.setProperty(SRB_USERNAME, "sal");
		defaults.setProperty(SRB_DOMAIN, "hpc.jcu.edu.au");
		defaults.setProperty(SRB_PASSWORD, "sjdu3iek");
		defaults.setProperty(SRB_HOST, "ngdata.hpc.jcu.edu.au");
		defaults.setProperty(SRB_RESOURCE, "jcuMainDisc");

		return defaults;
	}

}
